package com.zycfc.zsf.boot.mybatis.generator.codegen.db;

import com.zycfc.zsf.boot.mybatis.generator.codegen.exception.*;
import java.sql.*;
import java.util.*;

public class DaoHelper<T>
{
    private String url;
    private String userName;
    private String pwd;
    
    public String getUrl() {
        return this.url;
    }
    
    public void setUrl(final String url) {
        this.url = url;
    }
    
    public String getUserName() {
        return this.userName;
    }
    
    public void setUserName(final String userName) {
        this.userName = userName;
    }
    
    public String getPwd() {
        return this.pwd;
    }
    
    public void setPwd(final String pwd) {
        this.pwd = pwd;
    }
    
    public DaoHelper(final String url, final String username, final String password) {
        this.url = "";
        this.userName = "";
        this.pwd = "";
        this.url = url;
        this.userName = username;
        this.pwd = password;
    }
    
    public T queryForObject(final String sql, final MapCmd<T> cmd) throws CodegenException {
        Connection conn = null;
        Statement stmt = null;
        ResultSet rs = null;
        try {
            conn = DriverManager.getConnection(this.url, this.userName, this.pwd);
            stmt = conn.createStatement();
            rs = stmt.executeQuery(sql);
            if (rs.next()) {
                return cmd.getObjecFromRs(rs);
            }
            System.out.println("\u6ca1\u6709\u5230\u6570\u636e:" + sql);
            return null;
        }
        catch (SQLException e) {
            throw new CodegenException(e);
        }
        finally {
            try {
                if (rs != null) {
                    rs.close();
                }
                if (stmt != null) {
                    stmt.close();
                }
                if (conn != null) {
                    conn.close();
                }
            }
            catch (SQLException e2) {
                throw new CodegenException(e2);
            }
        }
    }
    
    public List<T> queryForList(final String sql, final MapCmd<T> cmd) throws CodegenException {
        Connection conn = null;
        Statement stmt = null;
        ResultSet rs = null;
        final List<T> list = new ArrayList<T>();
        try {
            conn = DriverManager.getConnection(this.url, this.userName, this.pwd);
            stmt = conn.createStatement();
            rs = stmt.executeQuery(sql);
            while (rs.next()) {
                list.add(cmd.getObjecFromRs(rs));
            }
        }
        catch (SQLException e) {
            throw new CodegenException(e);
        }
        finally {
            try {
                if (rs != null) {
                    rs.close();
                }
                if (stmt != null) {
                    stmt.close();
                }
                if (conn != null) {
                    conn.close();
                }
            }
            catch (SQLException e2) {
                throw new CodegenException(e2);
            }
        }
        return list;
    }
}
